package cn.buttheway.moodmusicdiary.activities;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.buttheway.moodmusicdiary.R;
import cn.buttheway.moodmusicdiary.adapters.MoodHistoryAdapter;
import cn.buttheway.moodmusicdiary.models.MoodEntry;
import cn.buttheway.moodmusicdiary.utils.DatabaseHelper;

public class MoodHistoryActivity extends AppCompatActivity {

    private RecyclerView rvMoodHistory;
    private MoodHistoryAdapter moodHistoryAdapter;
    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mood_history);

        rvMoodHistory = findViewById(R.id.rvMoodHistory);
        rvMoodHistory.setLayoutManager(new LinearLayoutManager(this));

        dbHelper = new DatabaseHelper(this);
        loadMoodHistory();
    }

    private void loadMoodHistory() {
        List<MoodEntry> moodEntries = dbHelper.getAllMoodEntries();
        moodHistoryAdapter = new MoodHistoryAdapter(moodEntries);
        rvMoodHistory.setAdapter(moodHistoryAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_mood_history, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_sort_date) {
            moodHistoryAdapter.sortByDate();
            return true;
        } else if (id == R.id.action_sort_sentiment) {
            moodHistoryAdapter.sortBySentiment();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}